package com.tzy.common.utils;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.auth0.jwt.interfaces.JWTVerifier;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import java.util.Calendar;
import java.util.Date;
@Slf4j
public class JwtUtil {
    private static final String USERNAME = "username";

    private static final String KEY = "123456qwe";

    /**
     * 过期时间（分钟）
     */
    private static final int EXPIRATION_TIME = 5;

    /**
     * 生成token
     *
     * @param userName
     * @return
     * @author 刘朋
     * <br/>date 2019-01-09
     */
    public static String createToken(String userName) {
        Algorithm algorithm = Algorithm.HMAC256(KEY);
//      指定token过期时间为7天
        Calendar expirationDate = Calendar.getInstance();
//        expirationDate.add( Calendar.MINUTE, EXPIRED_MINUTES );
        expirationDate.add(Calendar.MINUTE, 20);
//        Date expirationDate = DateUtil.addMinutes(DateUtil.getNow(), EXPIRATION_TIME);
        //生成token
        Date time = expirationDate.getTime();
        log.info("过期时间:{}",time);
        return JWT.create().withClaim(USERNAME, userName)
                .withExpiresAt(time)
                .sign(algorithm);
    }

    /**
     * 获取用户名
     *
     * @param token
     * @return
     * @author 刘朋
     * <br/>date 2019-03-08
     */
    public static String getUsername(String token) {
        return getValueByToken(token, USERNAME);
    }

    /**
     * 获取token中的值
     *
     * @param token
     * @param key
     * @return
     */
    public static String getValueByToken(String token, String key) {
        JWTVerifier verifier = JWT.require(Algorithm.HMAC256(KEY)).build();
        DecodedJWT decode = verifier.verify(token);
        return decode.getClaim(key).asString();
    }

    /**
     * 校验是否过期，过期返回true
     *
     * @param token
     * @return
     * @author 刘朋
     * <br/>date 2019-03-12
     */
    public static boolean isExpires(String token) {
        if (StringUtils.isBlank(token)) {
            return true;
        }
        JWTVerifier verifier = JWT.require(Algorithm.HMAC256(KEY)).build();
        try {
            verifier.verify(token);
        } catch (JWTVerificationException e) {
            return true;
        }
        return false;
    }
//https://blog.csdn.net/qq_37651267/article/details/99305364
}
